﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BL.BetterControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:_CheckBox Description=\"\" DivStyle=\"\"  DivType=\"\" LabelClass=\"\" runat=\"server\"></{0}:_CheckBox>")]
    public class _CheckBox : CheckBox
    {
        public string DivType { get { return ViewState["DivType"] != null ? (string)ViewState["DivType"] != "none" ? (string)ViewState["DivType"] : "block" : "block"; } set { ViewState["DivType"] = value; } }
        public string DivStyle { get { return ViewState["DivStyle"] != null ? (string)ViewState["DivStyle"] : ""; } set { ViewState["DivStyle"] = value; } }
        public string LabelClass { get { return ViewState["LabelClass"] != null ? (string)ViewState["LabelClass"] : ""; } set { ViewState["LabelClass"] = value; } }
        public string Description { get { return ViewState["Description"] != null ? (string)ViewState["Description"] : ""; } set { ViewState["Description"] = value; } }
        public bool LabelAfter { get { return ViewState["LabelAfter"] != null ? (bool)ViewState["LabelAfter"] : false; } set { ViewState["LabelAfter"] = value; } }
        public bool BlockType { get { return ViewState["BlockType"] != null ? (bool)ViewState["BlockType"] : false; } set { ViewState["BlockType"] = value; } }


        protected override void Render(HtmlTextWriter writer)
        {

            if (this.ToolTip == "")
                this.ToolTip = this.Description;

            if (DivType.ToLower() != "none")
            {
                writer.Write("<div");
                if (DivType != string.Empty)
                    writer.Write(" class=\"" + DivType + "\"");
                if (DivStyle != string.Empty)
                    writer.Write(" style=\"" + DivStyle + "\"");

                writer.Write(">");

            }

            string labelDisable = "";

            if (!this.Enabled)
                labelDisable = "disabled";

            //Fix för att inte checkboxen ska skriva ut en span
            string toolTip = this.ToolTip;
            this.ToolTip = "";


            if (BlockType)
            {

                // + this.Description + "</label>"
                writer.Write("<label class=\"blockCheck"  + LabelClass + " \"" + labelDisable + " for=\"" + this.ClientID + "\" title=\"" + toolTip + "\">");
                base.Render(writer);
                writer.Write("<span>" + Description + "</span></label>");


            }

            else
            {

                if (LabelAfter)
                {
                    base.Render(writer);
                }

                if (LabelClass != String.Empty)
                    writer.Write("<label class=\"" + LabelClass + "\" " + labelDisable + " for=\"" + this.ClientID + "\" title=\"" + toolTip + "\">" + this.Description + "</label>");
                else
                    writer.Write("<label for=\"" + this.ClientID + "\" " + labelDisable + " title=\"" + toolTip + "\">" + this.Description + "</label>");

                if (!LabelAfter)
                {
                    base.Render(writer);

                }

            }
           

            this.ToolTip = toolTip;



            if (DivType.ToLower() != "none")
                writer.Write("</div>"); ;
        }
    }
}
